package com.adtech.adms.system.web.action;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import com.adtech.adms.SessionKey;
import com.adtech.adms.commons.web.action.DataGridAction;
import com.adtech.adms.system.LogConstants;
import com.adtech.adms.system.SystemConstants.UserLoginLog;
import com.adtech.adms.system.pojo.User;
import com.adtech.core.dao.query.FilterItem;
import com.adtech.core.dao.query.FilterMap;
import com.adtech.core.dao.query.Pager;
import com.adtech.core.dao.query.QueryConstants;
import com.adtech.core.dao.query.Sort;
import com.adtech.core.dao.query.FilterItem.QueryType;
import com.adtech.core.exception.ServiceException;

/**
 * 登录日志action
 * 登录日志处理
 * @author Dormin 
 * @since 2011-6-15 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class LoginLogAction extends DataGridAction {

	private static final long serialVersionUID = 1L;


	private Long id;

	private UserLoginLog loginLog;
	
	private String fileName;
	
	private FileInputStream fileStream;
	




	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public UserLoginLog getLoginLog() {
		return loginLog;
	}

	public void setLoginLog(UserLoginLog loginLog) {
		this.loginLog = loginLog;
	}

	public String getFileName() {
		return fileName;
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public FileInputStream getFileStream() {
		return fileStream;
	}

	public void setFileStream(FileInputStream fileStream) {
		this.fileStream = fileStream;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}




	@Override
	protected List getModelDataGridList(Pager pager, FilterMap filtermap,
			Sort sort) {
		

		
		filtermap.put(LogConstants.QUERY_FIELD_STATUS, generateStatusCondition());
		if(sort!=null && sort.isEmpty()){
			sort.addSort(LogConstants.QUERY_FIELD_LOGINTIME,QueryConstants.DESC);
		}
		User user = (User)session.get(SessionKey.SESSION_KEY_USER);
		
		return null;
	}

	/**
	 * 
	 * @function 查看登录日志的时间范围内的系统日志
	 * @param
	 *
	 * @return
	 * @exception
	 *
	 * @author Dormin
	 * @history
	 *
	 */
	public String view() {
		//loginLog = systemLogService.getLoginLog(id);
		//session.put(SessionKey.LOGIN_LOG, loginLog);
		return SUCCESS;
	}

	/**
	 * 
	 * @function 组装查询条件，只查询状态为下线的用户登录日志
	 * @param
	 *
	 * @return 返回查询条件
	 * @exception
	 *
	 * @author Dormin
	 * @history
	 *
	 */
	private FilterItem generateStatusCondition() {
		FilterItem item = new FilterItem();
		item.setFilterName(LogConstants.QUERY_FIELD_STATUS);
		item.setFilterValue(Integer.toString(LogConstants.STATUS_LOGOUT));
		item.setExpType(QueryType.EQ.name());
		return item;
	}

//	/**
//	 * 导出日志文件生成excel
//	 * 
//	 * @return
//	 * @throws ServiceException
//	 * @throws UnsupportedEncodingException
//	 * @throws FileNotFoundException
//	 * @author Dormin
//	 */
//	public String exportLoginLog() throws ServiceException, UnsupportedEncodingException,
//			FileNotFoundException {
//		User user = (User)session.get(SessionKey.SESSION_KEY_USER);
//        getFilterMap().put(LogConstants.QUERY_FIELD_STATUS, generateStatusCondition());
//		String filePath = systemLogService.exportLoginLogList(user,getFilterMap().toExpression(), getSorter().addSort(LogConstants.QUERY_FIELD_LOGINTIME, QueryConstants.DESC));
//		fileName = URLEncoder.encode("登录日志", "UTF-8");
//		fileStream = new FileInputStream(filePath);
//
//		return SUCCESS;
//	}

}
